package ling.common;

import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.function.TriFunction;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
 * FieldInit;
 * @author : He-LiangLiang@Outlook.com
 * @date : 2025-3-26
 */
public class FieldInit {
    public void PowerStrSet(String parentName, Object obj, TriFunction<String, Field, Object, String> func) {
        if (obj == null)
            return;
        if (func == null)
            func = this::fmtValByName;
        var classz = obj.getClass();
        Field[] fields = classz.getDeclaredFields();
        for (Field field : fields) {
            if (!Modifier.isFinal(field.getModifiers()))
                continue;//Modifier.isFinal(field.getModifiers()) || Modifier.isStatic(field.getModifiers()) ||
            try {
                if (field.getType() == String.class) {
                    field.setAccessible(true);
                    var str=func.apply(parentName, field, obj);
                    field.set(obj, str);
                    powerList.add(str);
                } else if (field.getType().getClassLoader() != null)
                    PowerStrSet(func.apply(parentName, field, obj), field.get(obj), func);
            } catch (IllegalAccessException e) {
                System.out.println("属性设置异常");
                e.printStackTrace(System.out);

            }
        }
    }
    protected List<String> powerList=new ArrayList<>();

    public String[] gainList() {
        return powerList.toArray(new String[0]);
    }

    public String fmtValByName(String parentName, Field field, Object obj) {
       // var classz = field.getGenericType();
        if (StringUtils.isBlank(parentName))
            return  field.getName();
        return parentName + ":"  + field.getName();
    }


}
